<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="el-card_header">试题录入</div>
        <el-form ref="form" :model="form" label-width="100px" :rules="rules">
          <el-form-item label="学科：" prop="subjectID">
            <el-select v-model="form.subjectID" placeholder="请选择活动区域" @change="cheangeSub(form.subjectID)">
              <el-option :label="item.label" :value="item.value" v-for="item in subList" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录：" prop="catalogID">
            <el-select v-model="form.catalogID" placeholder="请选择活动区域">
              <el-option :label="item.label" :value="item.value" v-for="item in dirList" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="企业：" prop="enterpriseID">
            <el-select v-model="form.enterpriseID" placeholder="请选择活动区域">
              <el-option :label="item.shortName" :value="item.id" v-for="item in companysList" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="城市：" prop="city">
            <el-select v-model="form.province" clearable placeholder="请选择城市" @change="changeProvince(form.province)">
              <el-option v-for="item in provinces" :label="item" :key="item.id" :value="item"></el-option>
            </el-select>
            <el-select v-model="form.city" clearable placeholder="请选择地区" ref="province">
              <el-option v-for="item in citys" :label="item" :key="item.id" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向：" prop="direction">
            <el-select v-model="form.direction" placeholder="请选择活动区域">
              <el-option :label="item" :value="item" v-for="(item,index) in direction" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="题型：" prop="questionType">
            <el-radio-group v-model="form.questionType">
              <el-radio :value="item.label" :label="item.value" v-for="(item,index) in questionType" :key="index">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="难度：" prop="difficulty">
            <el-radio-group v-model="form.difficulty">
              <el-radio :value="item.label" :label="item.value" v-for="(item,index) in difficulty" :key="index">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="题干：" prop="question">
            <quill-editor v-model="form.question" ref="myQuillEditor"></quill-editor>
          </el-form-item>
          <el-form-item label="选项:" v-if="form.questionType!=='3'">

            <div class='float' v-for="(item,index) in form.options" :key="index">
              <el-radio v-if="form.questionType==='1'" :label="index" v-model="qtType">{{item.code}}:</el-radio>
              <el-checkbox v-else v-model="item.isRight" :label="item.code + ':'" />
              <el-input placeholder="请输入内容" v-model="item.title" class="optionsipt"></el-input>
              <!-- <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/">
                <img v-if="form.options.img" :src="form.options.img" class="avatar">
                <div class="text-qq">点击上传</div>
              </el-upload> -->
              <ImageUpload class="imgUpload" @uodataUrl="uodataUrl($event,index)"></ImageUpload>
            </div>
            <el-button :disabled="form.questionType === '1'" type="primary" plain @click="addQuestionType">+增加选项与答案</el-button>
          </el-form-item>
          <el-form-item label="解析视频：">
            <el-input v-model="form.videoURL"></el-input>
          </el-form-item>
          <el-form-item label="答案解析：" prop="answer">
            <quill-editor v-model="form.answer"></quill-editor>
          </el-form-item>
          <el-form-item label="题目备注：">
            <el-input type="textarea" v-model="form.remarks"></el-input>
          </el-form-item>
          <el-form-item label="试题标签：" prop="tags">
            <el-select v-model="Arrtags" placeholder="请选择活动区域" multiple @change="changeArr">
              <el-option :label="item.lebel" :value="item.label" v-for="item in tagsList" :key=item.value></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">确认提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import ImageUpload from '@/components/ImageUpload'
import { simple as subjectSimple } from '@/api/hmmm/subjects'
import { simple as dirSimple } from '@/api/hmmm/directorys'
import { list as cList } from '@/api/hmmm/companys'
import { citys, provinces } from '@/api/hmmm/citys'
import { direction, questionType, difficulty } from '@/api/hmmm/constants'
import { simple as tagsSimple } from '@/api/hmmm/tags'
import { add as questionsAdd } from '@/api/hmmm/questions'
export default {
  name: 'questionsNew',
  components: { ImageUpload },
  data () {
    return {
      // 标签处理数据
      Arrtags: [],
      // 增加的选项
      addQueType: ['E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
      // 单选控制
      qtType: '',
      // 难度
      difficulty: difficulty,
      // 题型
      questionType: questionType,
      // 方向
      direction: direction,
      // 城市下地区
      citys: citys(),
      // 所有城市
      provinces: provinces(),
      // 企业列表
      companysList: [],
      // 学科列表
      subList: [],
      // 目录列表
      dirList: [],
      // 标签列表
      tagsList: [],
      form: {
        // 学科
        subjectID: '',
        // 目录
        catalogID: '',
        // 企业
        enterpriseID: '',
        // 城市
        province: '',
        // 地区
        city: '',
        // 方向
        direction: '',
        // 题型
        questionType: '',
        // 难度
        difficulty: '',
        // 题干
        question: '',
        // 选项
        options: [{
          code: 'A', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案
        }, {
          code: 'B', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案
        }, {
          code: 'C', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案
        }, {
          code: 'D', // 代码
          title: '', // 标题
          img: '', // 图片URL
          isRight: false // 是否正确答案
        }],
        // 解析视频
        videoURL: '',
        // 答案解析
        answer: '',
        // 题目备注
        remarks: '',
        // 试题标签
        tags: ''

      },
      // 规则验证
      rules: {
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'change' }
        ],
        catalogID: [
          { required: true, message: '请选择目录', trigger: 'change' }
        ],
        enterpriseID: [
          { required: true, message: '请选择企业', trigger: 'change' }
        ],
        city: [
          { required: true, message: '请选择城市地区', trigger: 'change' }
        ],
        direction: [
          { required: true, message: '请选择方向', trigger: 'change' }
        ],
        questionType: [
          { required: true, message: '请选择题型', trigger: 'change' }
        ],
        difficulty: [
          { required: true, message: '请选择难度', trigger: 'change' }
        ],
        question: [
          { required: true, message: '请填写题干', trigger: 'blur' }
        ],
        answer: [
          { required: true, message: '请填写答案', trigger: 'blur' }
        ],
        tags: [{ required: true, message: '请选择标签', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 监听tags
    changeArr () {
      const ret = this.Arrtags.join()
      this.form.tags = ret
      console.log(this.form.tags)
    },
    // 增加选项与答案
    addQuestionType () {
      if (this.addQueType.length === 0) return this.$message.error('没有更多选项了')
      this.form.options.push({
        code: this.addQueType[0], // 代码
        title: '', // 标题
        img: '', // 图片URL
        isRight: false // 是否正确答案
      })
      this.addQueType.shift()
    },
    // 图片改变函数
    uodataUrl (url, index) {
      this.ruleForm.options[index].img = url
    },
    // 题型选项联动
    changeOptions () {
      this.form.questionType = this.questionType.find(item => item.label === this.qtType)
    },
    // 学科目录标签联动
    cheangeSub (sub) {
      const ret = dirSimple({ subjectID: sub })
      const ret1 = tagsSimple({ subjectID: sub })
      Promise.all([ret, ret1]).then(result => {
        this.dirList = result[0].data
        this.tagsList = result[1].data
      }).catch(() => {
        this.$message.error('获取信息失败')
      })
    },
    // 地区城市联动
    changeProvince (province) {
      this.citys = citys(province)
    },
    // 列表初始化
    loadSublist () {
      // 学科
      const ret1 = subjectSimple()
      // 企业
      const ret3 = cList()
      // 并发触发多个异步任务，Promise.all保证所有任务返回后得到所有的异步结果
      Promise.all([ret1, ret3]).then(result => {
        this.subList = result[0].data
        this.companysList = result[1].data.items
      }).catch(() => {
        this.$message.error('获取信息失败')
      }).finally(() => {
        this.loading = false
      })
    },
    // 提交表单
    onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          console.log(this.form)
          try {
            const ret = await questionsAdd(this.form)
            console.log(ret)
            if (ret.status === 200) {
              this.$message.success('提交成功')
              this.$refs.form.resetFields()
              this.$router.push('/questions/list')
            } else {
              this.$message.error('提交失败')
            }
          } catch (e) {
            console.log(e)
            this.$message.error('提交失败')
          }
        } else {
          this.$message.error('提交失败')
          return false
        }
      })
    }
  },
  created () {
    this.loadSublist()
  }
}
</script>
<style lang="scss" >
.imgUpload {
  position: relative;
  left: 300px;
  bottom: 45px;
  width: 100px;
}
.el-upload-list--picture-card .el-upload-list__item {
  display: inline-block;
  width: 100px;
  height: 60px;
}
.optionsipt {
  width: 220px;
}
.ql-editor {
  height: 220px !important;
}
.el-card_header {
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 20px;
}
.el-form {
  margin: 20px;
}
</style>
<style lang="scss" >
.avatar-uploader {
  display: inline-block;
  vertical-align: middle;
  margin-left: 10px;
  ::v-deep(.el-upload) {
    width: 100px;
    height: 60px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409eff;
    }
  }
}
.float {
  position: relative;
  padding-bottom: 20px;
  height: 80px;
}
